package com.lo.controller;

import com.github.pagehelper.PageInfo;
import com.lo.common.*;
import com.lo.entity.Course;
import com.lo.enums.EnumVip;
import com.lo.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("course")
public class CourseController {
    @Autowired
    private VideoInfo videoInfo;
    @Autowired
    private ImageInfo imageInfo;
    @Autowired
    private CourseService courseService;

    //上传课程
    @RequestMapping("load")
    public JsonResult addCourse(Course course,MultipartFile videoFile,MultipartFile videoCover){
        JsonResult videoResult = FileUploadUtils.upload(videoFile, videoInfo.getBasePath(),"video");
        JsonResult coverResult =  FileUploadUtils.upload(videoCover,imageInfo.getBasePath(),"cover");
//        System.out.println((String)coverResult.getData());
        course.setUrl((String) videoResult.getData());
        course.setCoverUrl((String)coverResult.getData());
        JsonResult result = courseService.addCourse(course);
        return result;
    }
    //删除课程
    @RequestMapping("deleteCourse/{id}")
    public JsonResult deleteCourse(@PathVariable Long id){
        JsonResult result = courseService.deleteCourse(id);
        Course course = (Course) result.getData();
        FileDeleteUtils.delete(videoInfo.getBasePath(),course.getUrl());
        FileDeleteUtils.delete(imageInfo.getBasePath(),course.getCoverUrl());

        return result;
    }
    //编辑框更新数据
    @RequestMapping("updateCourse")
    public JsonResult updateCourse(Course course){
        JsonResult result = courseService.updateCourse(course);
        return result;
    }
    //编辑框回显数据
    @RequestMapping("editCourse/{id}")
    public JsonResult editCourse(@PathVariable Long id){
        return courseService.editCourse(id);
    }
    //审核通过用
    @RequestMapping("updateIsExamine/{id}")
    public JsonResult updateIsExamine(@PathVariable Long id){
        return courseService.updateIsExamine(id);
    }
    //设置删除状态用
    @RequestMapping("updateIsDelete/{id}")
    public JsonResult updateIsDelete(@PathVariable Long id){
        return courseService.updateIsDelete(id);
    }
    //撤销删除用
    @RequestMapping("updateIsDelete2Zero/{id}")
    public JsonResult updateIsDelete2Zero(@PathVariable Long id){
        return courseService.updateIsDelete2Zero(id);
    }

    //普通课程数量
    @RequestMapping("courseOrdinaryCount")
    public JsonResult courseOrdinaryCount(){
        return courseService.courseOrdinaryCount();
    }
    //精品课程数量
    @RequestMapping("courseVipCount")
    public JsonResult courseVipCount(){
        return courseService.courseVipCount();
    }
    @RequestMapping("courseOrdinaryCountByTid/{tid}")
    public JsonResult courseOrdinaryCountByTid(@PathVariable Long tid){
        return courseService.courseOrdinaryCountByTid(tid);
    }
    @RequestMapping("courseVipCountByTid/{tid}")
    public JsonResult courseVipCountByTid(@PathVariable Long tid){
        return courseService.courseVipCountByTid(tid);
    }

    @RequestMapping("selectOrdinaryCourse")
    public JsonResult selectOrdinaryCourse(){
        JsonResult result = courseService.selectOrdinaryCourse();
        return result;
    }
    @RequestMapping("selectOrdinaryCourseWithPage")
    public JsonResult selectOrdinaryCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                   @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                   String search){
        PageInfo pageInfo = courseService.selectOrdinaryCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"免费课程里列表",pageInfo);
    }

    @RequestMapping("selectVipCourse")
    public JsonResult selectVipCourse(){
        JsonResult result = courseService.selectVipCourse();
        return result;
    }

    @RequestMapping("selectVipCourseWithPage")
    public JsonResult selectVipCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                              String search){
        PageInfo pageInfo = courseService.selectVipCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"精品课程里列表",pageInfo);
    }
    //工学课程相关查询
    @RequestMapping("selectEngineeringCourse")
    public JsonResult selectEngineeringCourse(){
        JsonResult result = courseService.selectEngineeringCourse();
        return result;
    }
    @RequestMapping("selectEngineeringCourseWithPage")
    public JsonResult selectEngineeringCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                      @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                      String search){
        PageInfo pageInfo = courseService.selectEngineeringCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"工学课程里列表",pageInfo);
    }
    //理学课程相关查询
    @RequestMapping("selectScienceCourse")
    public JsonResult selectScienceCourse(){
        JsonResult result = courseService.selectScienceCourse();
        return result;
    }
    @RequestMapping("selectScienceCourseWithPage")
    public JsonResult selectScienceCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                  @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                  String search){
        PageInfo pageInfo = courseService.selectScienceCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"理学课程里列表",pageInfo);
    }

    //文学课程相关查询
    @RequestMapping("selectLiteratureCourse")
    public JsonResult selectLiteratureCourse(){
        JsonResult result = courseService.selectLiteratureCourse();
        return result;
    }
    @RequestMapping("selectLiteratureCourseWithPage")
    public JsonResult selectLiteratureCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                  @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                  String search){
        PageInfo pageInfo = courseService.selectLiteratureCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"文学课程里列表",pageInfo);
    }
    //法学课程相关查询
    @RequestMapping("selectLawCourse")
    public JsonResult selectLawCourse(){
        JsonResult result = courseService.selectLawCourse();
        return result;
    }
    @RequestMapping("selectLawCourseWithPage")
    public JsonResult selectLawCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                                     @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                                     String search){
        PageInfo pageInfo = courseService.selectLawCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"法学课程里列表",pageInfo);
    }

    //法学课程相关查询
    @RequestMapping("selectAllCourse")
    public JsonResult selectAllCourse(){
        JsonResult result = courseService.selectAllCourse();
        return result;
    }
    @RequestMapping("selectAllCourseWithPage")
    public JsonResult selectAllCourseWithPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1")int pageNum,
                                              @RequestParam(value = "pageSize",required = false,defaultValue = "16")int pageSize,
                                              String search){
        PageInfo pageInfo = courseService.selectAllCourseWithPage(pageNum,pageSize,search);
        return new JsonResult(0,"法学课程里列表",pageInfo);
    }
    /**给微信小程序的API*/
    @RequestMapping("selectAllCourseToWX")
    public JsonResult selectAllCourseToWX(String search){
        return courseService.selectAllCourseToWX(search);
    }



}
